package com.moge.anjian.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

import android.widget.Toast;

import androidx.annotation.Nullable;

import com.moge.anjian.R;

import com.moge.anjian.activities.AnjianRuleActivity;
import com.moge.anjian.manager.AutoClickManager;
import com.moge.anjian.utils.MissionCallback;
import com.moge.anjian.utils.ScreenUtils;
import com.moge.anjian.manager.TargetManager;

public class AnjianHatchView extends LinearLayout {
    Context mContext;
    WindowManager manager;
    WindowManager.LayoutParams params;

    Button btnHatchStart;
    float initX, initY;
    float initTouchX, initTouchY;

    public AnjianHatchView(Context context) {
        super(context);
        this.mContext = context;
        manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        initView();
    }

    public AnjianHatchView(Context context, @Nullable AttributeSet attrs, Context mContext, WindowManager manager, WindowManager.LayoutParams params) {
        super(context, attrs);
        this.mContext = mContext;
    }

    public AnjianHatchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, Context mContext, WindowManager manager, WindowManager.LayoutParams params) {
        super(context, attrs, defStyleAttr);
        this.mContext = mContext;
        this.manager = manager;
        this.params = params;
    }

    public AnjianHatchView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, Context mContext, WindowManager manager, WindowManager.LayoutParams params) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.mContext = mContext;
        this.manager = manager;
        this.params = params;
    }

    @SuppressLint("MissingInflatedId")
    private void initView() {
        View rootView = LayoutInflater.from(getContext()).inflate(R.layout.view_hatch, this, true);

        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.TOP | Gravity.LEFT;

        btnHatchStart = findViewById(R.id.btn_hatch_start);
        AutoClickManager.getInstance().setOnMissionCallback(new MissionCallback() {
            @Override
            public void onMissionStart() {
                btnHatchStart.setBackgroundResource(R.drawable.ic_hatch_stop);
            }

            @Override
            public void onMissionStop() {
                btnHatchStart.setBackgroundResource(R.drawable.ic_hatch_pause);
            }
        });


        rootView.findViewById(R.id.btn_hatch_edit).setOnClickListener(v -> mContext.startActivity(new Intent(mContext, AnjianRuleActivity.class)));

        btnHatchStart.setOnClickListener(v -> {
            if (AutoClickManager.getInstance().isMissionRunning()) {
                Log.e("dimoge", "停止任务");
                AutoClickManager.getInstance().stopAutoClick();
                btnHatchStart.setBackgroundResource(R.drawable.ic_hatch_pause);
            } else {
                if (TargetManager.getInstance().getViewList().size() == 0) {
                    TargetManager.getInstance().addTargetView(mContext);
                    Toast.makeText(mContext, "移动浮标选择点击位置", Toast.LENGTH_SHORT).show();
                } else {
                    Log.e("dimoge", "启动任务任务");
                    TargetView targetView = TargetManager.getInstance().getViewList().get(0);
                    int[] targetPostion = new int[2];
                    targetView.getLocationOnScreen(targetPostion);
                    float x = Math.min(ScreenUtils.getScreenWidth(mContext),
                            targetPostion[0] + targetView.getWidth() / 2);
                    float y = Math.min(ScreenUtils.getScreenHeight(mContext),
                            targetPostion[1] + targetView.getHeight() / 2);
                    // 发起自动点击
                    AutoClickManager.getInstance().startAutoClick(
                            x,
                            y,
                            0,
                            200,
                            300,
                            0
                    );
                    btnHatchStart.setBackgroundResource(R.drawable.ic_hatch_stop);
                }
            }
        });

        rootView.findViewById(R.id.btn_hatch_add).setOnClickListener(v -> {
            if (TargetManager.getInstance().getViewList().size() == 0) {
                TargetManager.getInstance().addTargetView(mContext);
            } else {
                Toast.makeText(mContext, "暂不支持多点点击", Toast.LENGTH_SHORT).show();
            }
        });

        rootView.findViewById(R.id.btn_hatch_float).setOnTouchListener((v, event) -> {
            Log.e("dimoge", "onTouchEvent: x=" + event.getRawX());
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    initX = params.x;
                    initY = params.y;
                    initTouchX = event.getRawX();
                    initTouchY = event.getRawY();
                    return true;
                case MotionEvent.ACTION_MOVE:
                    params.x = (int) (initX + (event.getRawX() - initTouchX));
                    params.y = (int) (initY + (event.getRawY() - initTouchY));
                    manager.updateViewLayout(AnjianHatchView.this, params);
                    return true;
                default:
                    return false;
            }
        });

        rootView.findViewById(R.id.btn_hatch_close).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                removeWindow();
            }
        });

    }

    public void addWindow() {
        manager.addView(this, params);
    }

    public void removeWindow() {
        Log.e("dimoge", "移除悬浮: ");
        manager.removeViewImmediate(this);
    }

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        Log.e("dimoge", "onTouchEvent: y=" + event.getRawY());
//        switch (event.getAction()){
//            case MotionEvent.ACTION_DOWN:
//                initX = params.x;
//                initY = params.y;
//                initTouchX = event.getRawX();
//                initTouchY = event.getRawY();
//                return false;
//            case MotionEvent.ACTION_MOVE:
//                params.x = (int) (initX + (event.getRawX() - initTouchX));
//                params.y = (int) (initY + (event.getRawY() - initTouchY));
//                manager.updateViewLayout(this, params);
//                return false;
//            default:
//                return super.onTouchEvent(event);
//        }
//    }

}
